package com.harunasan.carrental.common.utils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.harunasan.carrental.common.query.CommonQueryParam;

/**
 * <p>
 * 查询对象封装操作类
 * </p>
 *
 * @author 梁毅
 * @since 2021/12/21
 */
public class CommonQueryWrapper<T> extends QueryWrapper<T> {
    /**
     * 构造方法
     *
     * @param commonQueryParam 公共查询类
     */
    public CommonQueryWrapper(CommonQueryParam commonQueryParam) {
        //查询生成
        this.queryGeneration(commonQueryParam);
        //排序生成
        this.sortGeneration(commonQueryParam);
    }

    /**
     * 查询生成
     *
     * @param commonQueryParam 公共查询类
     */
    private void queryGeneration(CommonQueryParam commonQueryParam) {
        if (commonQueryParam.getQueryParams() == null) {
            return;
        }

        for (CommonQueryParam.QueryParam queryParam : commonQueryParam.getQueryParams()) {

            if (queryParam.getVal() != null) {
                switch (queryParam.getSelectCondition()) {
                    case "<":
                        this.lt(queryParam.getColumnName(), queryParam.getVal().get(0));
                        break;
                    case "<=":
                        this.le(queryParam.getColumnName(), queryParam.getVal().get(0));
                        break;
                    case ">":
                        this.gt(queryParam.getColumnName(), queryParam.getVal().get(0));
                        break;
                    case ">=":
                        this.ge(queryParam.getColumnName(), queryParam.getVal().get(0));
                        break;
                    case "=":
                        this.eq(queryParam.getColumnName(), queryParam.getVal().get(0));
                        break;
                    case "!=":
                        this.ne(queryParam.getColumnName(), queryParam.getVal().get(0));
                        break;
                    case "like":
                        this.like(queryParam.getColumnName(), queryParam.getVal().get(0));
                        break;
                    case "likeLeft":
                        this.likeLeft(queryParam.getColumnName(), queryParam.getVal().get(0));
                        break;
                    case "likeRight":
                        this.likeRight(queryParam.getColumnName(), queryParam.getVal().get(0));
                        break;
                    case "in": {
                        this.in(queryParam.getColumnName(), queryParam.getVal());
                    }
                    break;
                    case "notIn": {
                        this.notIn(queryParam.getColumnName(), queryParam.getVal());
                    }
                    break;
                    case "between": {
                        this.between(queryParam.getVal().size() == 2, queryParam.getColumnName(), queryParam.getVal().get(0), queryParam.getVal().get(1));
                    }
                    break;
                    default:
                        break;
                }
            }
        }
    }

    /**
     * 排序生成
     *
     * @param commonQueryParam 公共查询类
     */
    private void sortGeneration(CommonQueryParam commonQueryParam) {
        if (commonQueryParam.getIsAsc() == null || commonQueryParam.getSortColumns() == null) {
            return;
        }
        this.orderBy(commonQueryParam.getSortColumns() != null, commonQueryParam.getIsAsc(), commonQueryParam.getSortColumns());
    }
}
